package com.zhong.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zhong.auth.application.convert.AuthPermissionDTOConvert;
import com.zhong.auth.application.dto.AuthPermissionDTO;
import com.zhong.auth.common.entity.Result;
import com.zhong.auth.domain.bo.AuthPermissionBO;
import com.zhong.auth.domain.service.AuthPermissionDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 权限controller
 */
@Slf4j
@RestController
@RequestMapping("/permission")
public class AuthPermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 新增权限
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthPermissionController.add.authPermissionDTO: {}", JSON.toJSONString(authPermissionDTO));
            }
            //数据校验
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()),"权限名称不能为空！！！");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(),"父类id不能为空！！！");
            Preconditions.checkNotNull(authPermissionDTO.getType(),"权限类型不能为空！！！");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConvert.INSTANCE.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        }catch (Exception e){
            log.error("AuthPermissionController.add.error: {}",e.getMessage(),e);
            return Result.fail("新增权限失败");
        }
    }

    /**
     * 修改权限
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthPermissionController.update.authPermissionDTO: {}", JSON.toJSONString(authPermissionDTO));
            }
            //数据校验
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空！！！");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConvert.INSTANCE.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        }catch (Exception e){
            log.error("AuthPermissionController.add.error: {}",e.getMessage(),e);
            return Result.fail("修改权限失败");
        }
    }

    /**
     * 删除权限
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthPermissionController.delete.authPermissionDTO: {}", JSON.toJSONString(authPermissionDTO));
            }
            //数据校验
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空！！！");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConvert.INSTANCE.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        }catch (Exception e){
            log.error("AuthPermissionController.add.error: {}",e.getMessage(),e);
            return Result.fail("删除权限失败");
        }
    }
    /**
     * 权限的禁用和启用
     */

}
